package com.y3k.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.y3k.utils.ConnectionHelper;
import org.apache.log4j.Logger;

public class QueryDao {

	public static final Logger log = Logger.getLogger(QueryDao.class);

	public List<Object> execute(String query) throws Exception {
		List<Object> lista = new ArrayList<Object>();

		Connection conn = ConnectionHelper.getConnection();
		Statement stmt = conn.createStatement();

		log.info("Ejecutando [" + query + "]");
		ResultSet rs = stmt.executeQuery(query);
		while (rs.next()) {
			ResultSetMetaData metaData = rs.getMetaData();
			Object[] data = new Object[metaData.getColumnCount()];
			for (int i = 0; i < metaData.getColumnCount(); i++) {
				data[i] = rs.getObject(i + 1);
			}
			lista.add(data);
		}
		stmt.close();
		log.info("Resultados [" + lista.size() + "]");

		return lista;
	}
}